package atmgui;

import atm.FileUtil;
import atm.User;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 改密
 */

public class ChangePwdPanel extends JPanel implements ActionListener {
    JTextField usrTextField1 = new JFormattedTextField();
    JTextField usrTextField2 = new JFormattedTextField();
    JTextField usrTextField3 = new JFormattedTextField();
    private JButton offbtn=new JButton(new ImageIcon("src/images/buttonOff.jpg"));



    private ATM05 atm05;
    private Font font =new Font("宋体",Font.PLAIN,25);
    private JButton surebtn=new JButton(new ImageIcon("src/images/buttonSure.jpg"));

    public  ChangePwdPanel(ATM05 atm05) {



        JLabel label1 = new JLabel("请输入原密码");
        label1.setBounds(100, 200, 300, 50);
        label1.setFont(font);//字体大小
        label1.setForeground(Color.BLUE);//字体颜色


        JLabel label2 = new JLabel("请输入新密码");
        label2.setBounds(100, 300, 300, 50);
        label2.setFont(font);//字体大小
        label2.setForeground(Color.BLUE);//字体颜色


        usrTextField1.setBounds(300, 200, 300, 50);


        JLabel label3 = new JLabel("请再次输入新密码");
        label3.setBounds(100, 400, 300, 50);
        label3.setFont(font);//字体大小
        label3.setForeground(Color.BLUE);//字体颜色


        usrTextField2.setBounds(300, 300, 300, 50);

        //文本框组件

        usrTextField3.setBounds(300, 400, 300, 50);









        surebtn.setBounds(250, 500, 140, 50);
        surebtn.addActionListener(this);
        offbtn.setBounds(500,500,140,50);
        offbtn.addActionListener(this);

        this.add(offbtn);
        this.add(surebtn);
        this.add(label1);
        this.add(label2);
        this.add(label3);
        this.add(usrTextField1);
        this.add(usrTextField2);
        this.add(usrTextField3);


        this.atm05 = atm05;
        this.setLayout(null);
        this.setOpaque(false);


    }



        @Override
    public void actionPerformed(ActionEvent e) {

        if (e.getSource()==surebtn){
            User user=atm05.getLoginUser();
            String user1=user.getPwd();

            if (user1.equals(usrTextField1.getText())){
                String user2= usrTextField2.getText();
                String user3=usrTextField3.getText();

                if (user2.equals(user3)){
                    user.setPwd(user2);
                    JOptionPane.showMessageDialog(this, "修改成功");
                }else {
                    JOptionPane.showMessageDialog(this, "两次密码不一样");

                }

            }else {
                JOptionPane.showMessageDialog(this,"输入原密码错误");

            }
            usrTextField1.setText("");
            usrTextField2.setText("");
            usrTextField3.setText("");
            FileUtil.writeObject(atm05.getUsers(),ATM05.FILE_PATH);
            atm05.showPanel("menu");



        }else if(e.getSource()==offbtn){
            atm05.showPanel("menu");
        }



    }
}
